import { ref, computed } from 'vue'
//  列表选中功能
const useTableSelect = () => {
  const selectObj = ref({})
  const isSelect = computed(() => (v) => {
    return Object.keys(selectObj.value).includes(String(v))
  })
  let isSelectAll = ref(false)

  const handleSelect = (row, Amount) => {
    let ishave = Object.keys(selectObj.value).includes(String(row.pkid))
    if (ishave) {
      delete selectObj.value[row.pkid]
    } else {
      selectObj.value = {
        ...selectObj.value,
        [row.pkid]: row,
      }
    }
    isSelectAll.value = Object.keys(selectObj.value).length == Amount
  }
  const handleSelectAll = (DataList) => {
    if (isSelectAll.value) {
      selectObj.value = {}
    } else {
      let objArr = {}
      DataList.map((item) => {
        objArr = { ...objArr, [item.pkid]: item }
      })
      selectObj.value = {
        ...selectObj.value,
        ...objArr,
      }
    }
    isSelectAll.value = !isSelectAll.value
  }
  const handleSelectAdd = (DataList) => {
    let objArr = {}
    DataList.map((item) => {
      objArr = { ...objArr, [item.pkid]: item }
    })
    selectObj.value = {
      ...selectObj.value,
      ...objArr,
    }
  }
  const setNullSelect = () => {
    selectObj.value = {}
    isSelectAll.value = false
  }

  return {
    selectObj,
    isSelect,
    isSelectAll,
    handleSelect,
    handleSelectAll,
    setNullSelect,
    handleSelectAdd,
  }
}

export { useTableSelect }
